package cosplay.Servlet;
import java.io.IOException;

import cosplay.DAO.AlbumDAOImpl;
import cosplay.DAO.PhotoDAOImpl;
import cosplay.DAO.CompetitionDetailDAOImpl;
import cosplay.DAO.UserDAOImpl;
import cosplay.Services.CommonServices;
import cosplay.Services.RandImgCreater;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class Jpgservlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4694869124093901168L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		ApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		UserDAOImpl userdao=(UserDAOImpl) context.getAutowireCapableBeanFactory().getBean("userdao");//.createBean(cosplay.DAO.UserDAOImpl.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,true);
		PhotoDAOImpl photodao=(PhotoDAOImpl) context.getAutowireCapableBeanFactory().getBean("photodao");//.createBean(cosplay.DAO.PhotoDAOImpl.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,true);
		CompetitionDetailDAOImpl resultdao=(CompetitionDetailDAOImpl) context.getAutowireCapableBeanFactory().getBean("detaildao");//.createBean(cosplay.DAO.CompetitionDetailDAOImpl.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,true);
		AlbumDAOImpl albumdao=(AlbumDAOImpl) context.getAutowireCapableBeanFactory().getBean("albumdao");//.createBean(cosplay.DAO.AlbumDAOImpl.class,AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE,true);
		String name=req.getRequestURI();
		name=name.substring(name.lastIndexOf("/")+1);
		name=name.replaceAll(".jpg", "");	//work
		boolean b=name.equals("cod");		//Not shown in the Variable List when debugging
		byte[] jpg =null;					//work
		if (name.startsWith("preview_"))	//return properly
		{
			name=name.replaceAll("preview_", "");
			jpg=photodao.getPreview(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("photo_"))	//return properly
		{
			name=name.replaceAll("photo_", "");
			jpg=photodao.getImage(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("head_"))	//return properly
		{
			name=name.replaceAll("head_", "");
			jpg=userdao.getHeadImage(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("result_"))	//return properly
		{
			name=name.replaceAll("result_", "");
			jpg=resultdao.getCover(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("resultpre_"))	//return properly
		{
			name=name.replaceAll("resultpre_", "");
			jpg=resultdao.getCoverpreview(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("album_"))	//return properly
		{
			name=name.replaceAll("album_", "");
			jpg=albumdao.getCover(CommonServices.stringIntCheck(name));
		}
		if (name.startsWith("albumpre_"))	//return properly
		{
			name=name.replaceAll("albumpre_", "");
			jpg=albumdao.getCoverPreview(CommonServices.stringIntCheck(name));
		}
		if (b)								//no return
		{
			RandImgCreater rc = new RandImgCreater();
			Object[] obj=rc.createRandImage();
			String rand = (String) obj[0];
			jpg=(byte[]) obj[1];
			req.getSession().setAttribute("rand",rand);
		}
		if (jpg!=null)
			resp.getOutputStream().write(jpg);
		else
			resp.getOutputStream().println("photo not found");
	}
}
